package com.briup.sms.service.impl;

import com.briup.sms.dao.BannerMapper;
import com.briup.sms.entity.Banner;
import com.briup.sms.service.BannerService;
import com.briup.sms.utils.ResultCode;
import com.briup.sms.utils.SMSException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper mapper;

    @Override
    public PageInfo<Banner> findBanners(String title, Boolean active, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Banner> list = mapper.findBanners(title, active);
        PageInfo<Banner> info = new PageInfo<>(list);
        return info;
    }

    @Override
    //根据具体数据情况来判断是要插入还是更新
    public void save(Banner banner) {
        if (banner.getId() == null){
            mapper.save(banner);
        }else{
            mapper.update(banner);
        }
    }

    @Override
    public void updateActive(Integer id) {
        //先查询指定id对应的轮播的状态
        Banner banner = mapper.findById(id);
        if (banner== null)
            throw  new SMSException(ResultCode.DATA_NONE);
        //原来的启用状态取反
        Boolean b = !banner.getActive();
        banner.setActive(b);
        //修改对象的数据
        mapper.update(banner);

    }


}
